package com.holystone.orca.itsm.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.holystone.orca.itsm.system.model.SysUser;
import com.holystone.orca.itsm.system.service.SysUserService;
import com.holystone.orca.itsm.system.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {
    @Override
    public SysUser login(String username, String password) {
        Example emp = new Example(SysUser.class);
        emp.createCriteria()
                .andEqualTo("userCode",username)
                .andEqualTo("userPwd",password);
        List<SysUser> users = selectByExample(emp);
        if(users==null||users.size()==0){
            return null;
        }
        return users.get(0);
    }

    @Override
    public List<SysUser> findSysUser(SysUser user) {
        Example emp = new Example(SysUser.class);
        Example.Criteria criteria = emp.createCriteria();
        if(user.getUserId() != null){
            criteria.andEqualTo("userId",user.getUserId());
        }
        if(user.getDelFlag() != null){
            criteria.andEqualTo("delFlag",user.getDelFlag());
        }
        if(user.getIsSuper() != null){
            criteria.andEqualTo("isSupper",user.getIsSuper());
        }
        if(user.getUserCode()!= null && !user.getUserCode().equals("")){
            criteria.andLike("userCode",user.getUserCode());
        }
        if(user.getUserEmail() != null && !user.getUserEmail().equals("")){
            criteria.andLike("userEmail",user.getUserEmail());
        }
        if (user.getUserName() != null && !user.getUserName().equals("")) {
            criteria.andLike("userName",user.getUserName());
        }
        if(user.getUserPhone() !=null && !user.getUserPhone().equals("")){
            criteria.andLike("userPhone",user.getUserPhone());
        }
        return selectByExample(emp);
    }

    @Override
    public PageInfo<SysUser> pagingSysUser(SysUser user, Integer pageNO, Integer pageSize) {
        PageHelper.startPage(pageNO,pageSize);
        PageInfo<SysUser> pageInfo = new PageInfo<>(findSysUser(user));
        return pageInfo;
    }

    @Override
    public SysUser addSysUser(SysUser sysUser) {
        int id = save(sysUser);
        sysUser.setUserId(id);
        return sysUser;
    }
}
